package com.epoint.mobileoa.ui.notice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebSettings.ZoomDensity;
import android.widget.Button;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetNoticeContentTask;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.model.Attach;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.StringHelp;

public class NoticeDetailView extends SuperView {

	private WebView wv;
	private List<Attach> attachList = new ArrayList<Attach>();//附件列表
	private String saveguid;

	private String intenttype ;

	private int taskid;

	private Button btFkxx;
	
	private String guid;
	@Override
	public void initControls(){
		intenttype = getIntent().getStringExtra("intenttype");
		intenttype = intenttype==null?"":intenttype;

		wv = (WebView) findViewById(R.id.nd_wv);
		
		//支持JS
		wv.getSettings().setJavaScriptEnabled(true);
		//设置背景色
		wv.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
		//显示放大缩小 controler
		wv.getSettings().setBuiltInZoomControls(true); 
		//可以缩放
		wv.getSettings().setSupportZoom(true); 
		//默认缩放模式
		wv.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
		top_right_bt.setOnClickListener(this);

		taskid = 1;
		top_pb.setVisibility(View.VISIBLE);
		String pagetitle = getIntent().getStringExtra("pagetitle");
		top_title.setText(pagetitle);
		
		guid = getIntent().getStringExtra("guid");
		HashMap<String,Object> param=new HashMap<String,Object>();
		param.put("guid", guid);
		Task task = new GetNoticeContentTask(this, param);
		MainService.addTask(task);
		saveguid = guid;
	}


	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskid==1){
				String bs = param[1].toString();
				String title = getIntent().getStringExtra("title");
				String sender = getIntent().getStringExtra("sender");
				String time = StringHelp.getXMLAtt(bs, "time");
				String contents = StringHelp.getXMLAtt(bs, "contents");
				String fontcolor = "black";
				
				String html = "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td><font color="+fontcolor+" size=2>标题："+title+"</font></td></tr><tr ><td><hr/></td></tr><tr><td><font color="+fontcolor+" size=2>发送时间："+time+"</font></td></tr><tr ><td><hr/></td></tr><tr><td><font color="+fontcolor+" size=2>发稿人："+sender+"</font></td></tr><tr ><td><hr/></td></tr></tr>";
				html += "<tr><td>"+contents+"</td></tr>";
				html += "</table>";
				wv.loadDataWithBaseURL(null, html,"text/html","utf-8",null);
				String attaches = StringHelp.getXMLAttOut(bs, "attaches");
				attachList = XMLHelp.AttachListAna(attaches);
				String hasattach = getIntent().getStringExtra("hasattach");
				if(hasattach.equals("1")){
					top_right_bt.setVisibility(View.VISIBLE);
					top_right_bt.setText("附件");
//					top_right_bt.setBackgroundResource(R.drawable.ic_btn_attach_selector);
				}
				
				String fkNums = StringHelp.getXMLAtt(bs, "fkNums");
//				if(!fkNums.equals("0")){
					findViewById(R.id.llBottom).setVisibility(View.VISIBLE);
					btFkxx = (Button)findViewById(R.id.btFkxx);
					String btFkxxText = "信息反馈";
					if(!fkNums.equals("0")){
						btFkxxText += "("+fkNums+")";
					}
					btFkxx.setText(btFkxxText);
					btFkxx.setOnClickListener(this);
//				}
				
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	@Override
	public void onClick(View v) {
		if(v==top_right_bt){
			AlertHelp.showAttachDownloadAlert(this, attachList, saveguid);
		}
		else if(v==btFkxx){
			Intent intent = new Intent(this, FeedBackView.class);
			intent.putExtra("infoguid",guid);
			startActivity(intent);
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.noticedetail;
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return super.onCreateOptionsMenu(menu);
	}



	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
